<?php
/**
 * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说/图/谱/源/码/分/享
 */
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24 0024
 * Time: 上午 11:14
 */
defined("IN_IA")or exit("Access Denied");
checklogin();  //验证是否登录
global $_W,$_GPC;
$uniacid=$_W['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] : "land_type_list";
//土地分类列表
if($op=='land_type_list'){
    $condition=array();
    $condition['uniacid']=$uniacid;
    if($_GPC['name']){
        $name=$_GPC['name'];
        $condition['name LIKE']='%'.$name.'%';
    }
    $listCount=pdo_getall("cqkundian_farm_land_type",$condition);
    $total=count($listCount);   //数据的总条数
    $pageSize=10; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_land_type",$condition,'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/land/land_type");
}
//新增编辑土地分类
if($op=='land_type_edit'){
    if($_GPC['id']){
        $list=pdo_get('cqkundian_farm_land_type',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
        $list['slide']=unserialize($list['slide']);
    }
    include $this->template("web/land/land_edit");
}

//保存土地分类
if($op=='land_type_save'){
    $data=array(
        'name'=>$_GPC['name'],
        'rank'=>$_GPC['rank'],
        'uniacid'=>$uniacid,
    );
    $slide=$_GPC['slide'];
    for($i=0;$i<count($slide);$i++){
        $slide[$i]=tomedia($slide[$i]);
    }
    $data['slide']=serialize($slide);
    if(empty($_GPC['id'])){  //新增
        $request=pdo_insert("cqkundian_farm_land_type",$data);
    }else{
        $condition=array(
            'id'=>$_GPC['id'],
            'uniacid'=>$uniacid,
        );
        $request=pdo_update("cqkundian_farm_land_type",$data,$condition);
    }
    if($request){
        message("操作成功",$this->createWebUrl("land"));
    }else {
        message("操作失败", '', 'warning');
    }
}

//删除土地分类信息
if($op=='land_type_delete'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_land_type',array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//土地列表
if($op=='land_list'){
    $condition=array();
    $condition['uniacid']=$uniacid;
    if($_GPC['land_name']){
        $land_name=$_GPC['land_name'];
        $condition['land_name LIKE']='%'.$land_name.'%';
    }
    $listCount=pdo_getall("cqkundian_farm_land",$condition);
    $total=count($listCount);   //数据的总条数
    $pageSize=10; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_land",$condition,'','','rank asc',array($pageIndex,$pageSize));
    for($i=0;$i<count($list);$i++){
        $type=pdo_get('cqkundian_farm_land_type',array('id'=>$list[$i]['type_id'],'uniacid'=>$uniacid));
        $list[$i]['name']=$type['name'];
    }
    include $this->template("web/land/land_list");
}

//土地新增编辑
if($op=='land_edit'){
    $typeData=pdo_getall('cqkundian_farm_land_type',array('uniacid'=>$uniacid));
    $seedData=pdo_getall('cqkundian_farm_send',array('uniacid'=>$uniacid));
    if($_GPC['id']){
        $list=pdo_get('cqkundian_farm_land',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
        $list['seed']=explode(',',$list['seed']);
        for($i=0;$i<count($seedData);$i++){
            for($j=0;$j<count($list['seed']);$j++){
                if($list['seed'][$j]==$seedData[$i]['id']){
                    $seedData[$i]['is_checked']=1;
                }
            }
        }
    }

    //查询监控信息
    $liveData=pdo_getall('cqkundian_farm_live',array('uniacid'=>$uniacid));

    include $this->template("web/land/land_list_edit");
}

if($op=='land_list_save'){
    $data=array(
        'cover'=>tomedia($_GPC['cover']),
        'land_name'=>$_GPC['land_name'],
        'all_area'=>$_GPC['all_area'],
        'residue_area'=>$_GPC['residue_area'],
        'area'=>$_GPC['area'],
        'price'=>$_GPC['price'],
        'type_id'=>$_GPC['type_id'],
        'rank'=>$_GPC['rank'],
        'live_id'=>$_GPC['live_id'],
        'uniacid'=>$uniacid,
    );
    $seed=implode(',',$_GPC['seed']);
    $data['seed']=$seed;
    if(empty($_GPC['id'])){  //新增
        $request=pdo_insert("cqkundian_farm_land",$data);
    }else{
        $condition=array(
            'id'=>$_GPC['id'],
            'uniacid'=>$uniacid,
        );
        $request=pdo_update("cqkundian_farm_land",$data,$condition);
    }
    if($request){
        $url=url('site/entry/land',array('m'=>'cqkundian_farm','op'=>'land_list'));
        message("操作成功",url('site/entry/land',array('m'=>'kundian_farm','op'=>'land_list')));
    }else {
        message("操作失败", '', 'warning');
    }
}

//土地删除
if($op=='land_delete'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_land',array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//租地年限设置
if($op=='land_limit'){
    $condition=array();
    $condition['uniacid']=$uniacid;
    $lid=$_GPC['id'];
    $condition['lid']=$_GPC['id'];
    $listCount=pdo_getall("cqkundian_farm_land_buy_limit",$condition);
    $total=count($listCount);   //数据的总条数
    $pageSize=10; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_land_buy_limit",$condition,'','','rank asc',array($pageIndex,$pageSize));
    include $this->template("web/land/land_limit_list");
}

if($op=='land_limit_edit'){
    $lid=$_GPC['lid'];
    if($_GPC['id']){
        $list=pdo_get('cqkundian_farm_land_buy_limit',array('id'=>$_GPC['id'],'uniacid'=>$uniacid));
    }
    include $this->template("web/land/land_limit_edit");
}

if($op=='land_limit_save'){
    $data=array(
        'price'=>$_GPC['price'],
        'day'=>$_GPC['day'],
        'status'=>$_GPC['status'],
        'rank'=>$_GPC['rank'],
        'uniacid'=>$uniacid,
        'lid'=>$_GPC['lid'],
    );
    if(empty($_GPC['id'])){  //新增
        $request=pdo_insert("cqkundian_farm_land_buy_limit",$data);
    }else{
        $condition=array(
            'id'=>$_GPC['id'],
            'uniacid'=>$uniacid,
        );
        $request=pdo_update("cqkundian_farm_land_buy_limit",$data,$condition);
    }
    if($request){
        $url=url('site/entry/land', array('m' => 'kundian_farm','op'=>'land_list'));
        message("操作成功",$url);
    }else {
        message("操作失败", '', 'warning');
    }
}

if($op=='land_limit_changeStatus'){
    $id=$_GPC['id'];
    $res=pdo_update('cqkundian_farm_land_buy_limit',array('status'=>$_GPC['status']),array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}
//删除
if($op=='land_limit_delete'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_land_buy_limit',array('id'=>$id,'uniacid'=>$uniacid));
    echo $res ? json_encode(array('status'=>1)) : json_encode(array('status'=>2));die;
}

//土地购买订单
if($op=='land_order'){

    $condition=array();
    if(!empty($_GPC['order_number'])){
        $order_number=trim($_GPC['order_number']);
        $condition['order_number LIKE']= '%'.$order_number.'%';
    }
    $status=$_GPC['status'] ?$_GPC['status'] :5;    //订单状态  1/全部订单 2/未支付 3/待发货 4/待收货 5/已完成 6/已取消
    if($status!=5){
        $condition['status']=$status;
    }
    $time=$_GPC['time'];
    if($time){
        $condition['create_time >']=strtotime($time['start']);
        $condition['create_time <']=strtotime($time['end']);
    }
    $condition['uniacid']=$uniacid;
    $listCount=pdo_getall("cqkundian_farm_land_order",$condition);
    $moneyData=pdo_getall("cqkundian_farm_land_order",array('uniacid'=>$uniacid,'status'=>1));
    $totalMoney=0;
    for ($i=0;$i<count($moneyData);$i++){
        $totalMoney+=$moneyData[$i]['total_price'];
    }
    $total=count($listCount);
    $pageSize=15; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_land_order",$condition,'','','create_time desc',array($pageIndex,$pageSize));
    include $this->template('web/land/land_order');
}

//土地购买订单详情
if($op=='land_order_edit'){
    $id=$_GPC['id'];
    $orderData=pdo_get('cqkundian_farm_land_order',array('id'=>$id,'uniacid'=>$uniacid));
    $orderDetail=pdo_getall('cqkundian_farm_land_order_detail',array('order_id'=>$id,'uniacid'=>$uniacid));
    include $this->template('web/land/land_order_edit');
}

//删除订单信息
if($op=='land_order_delete'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_land_order',array('id'=>$id,'uniacid'=>$uniacid));
    $detail_res=pdo_delete('cqkundian_farm_land_order_detail',array('order_id'=>$id,'uniacid'=>$uniacid));
    if($res&& $detail_res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}

//购买种子订单
if($op=='send_order'){
    $condition=array();
    if(!empty($_GPC['order_number'])){
        $order_number=trim($_GPC['order_number']);
        $condition['order_number LIKE']= '%'.$order_number.'%';
    }
    $status=$_GPC['status'] ?$_GPC['status'] :5;    //订单状态  1/全部订单 2/未支付 3/待发货 4/待收货 5/已完成 6/已取消
    if($status!=5){
        $condition['status']=$status;
    }
    $time=$_GPC['time'];
    if($time){
        $condition['create_time >']=strtotime($time['start']);
        $condition['create_time <']=strtotime($time['end']);
    }
    $condition['uniacid']=$uniacid;
    $listCount=pdo_getall("cqkundian_farm_send_order",$condition);
    $totalMoney=0;
    for ($i=0;$i<count($listCount);$i++){
        $totalMoney+=$listCount[$i]['total_price'];
    }
    $total=count($listCount);
    $pageSize=15; //每页显示的数据条数
    $pageIndex=intval($_GPC['page']) ? intval($_GPC['page']) :1;  //当前页
    $pager=pagination($total,$pageIndex,$pageSize);
    $list=pdo_getall("cqkundian_farm_send_order",$condition,'','','create_time desc',array($pageIndex,$pageSize));
    include $this->template('web/land/send_order');
}

//种子购买编辑
if($op=='send_order_edit'){
    $id=$_GPC['id'];
    $orderData=pdo_get('cqkundian_farm_send_order',array('id'=>$id,'uniacid'=>$uniacid));
    $orderDetail=pdo_getall('cqkundian_farm_send_order_detail',array('order_id'=>$id,'uniacid'=>$uniacid));
    include $this->template('web/land/send_order_edit');
}

if($op=='send_order_delete'){
    $id=$_GPC['id'];
    $res=pdo_delete('cqkundian_farm_send_order',array('id'=>$id,'uniacid'=>$uniacid));
    $detail_res=pdo_delete('cqkundian_farm_send_order_detail',array('order_id'=>$id,'uniacid'=>$uniacid));
    if($res&& $detail_res){
        echo json_encode(array('status'=>1));die;
    }else{
        echo json_encode(array('status'=>2));die;
    }
}
























